using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Auto.Data.Security;
using BLL = Auto.Data.Systems.BLL;
using Model = Auto.Data.Systems.Model;
using Auto.Web.Common;
using Auto.Components;
using Auto.Data.Setting;
using Auto.Data.Common;

namespace BaseSystem._System
{
    public partial class SysFuncOperates : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ucSysFuncOperates1.OperatiOnClick += new BaseSystem._System.UserControl.ucSysFuncOperates.OperatiOnClickEventHandler(OperatiOnClick);
        }
        protected void OperatiOnClick(string value)
        {
            divGridView.Visible = true;
            Auto.Data.Systems.BLL.SysFunctions funcs = new Auto.Data.Systems.BLL.SysFunctions();

            DataTable dt = funcs.GetFuncsAndOperate(SessionBox.CultrueInfo, "", value);

            BindGridView(dt);
            CheckOperateList(dt);
        }
        private void BindGridView(DataTable dt)
        {
            this.gv.PageSize = SessionBox.PageSize;  
            this.gv.DataSource = dt.DefaultView.ToTable(true, new string[] { Model.SysFunctions.FIELD_SysID, 
                                                        Model.SysFunctions.FIELD_FuncName, Model.SysFunctions.FIELD_FuncID });
            this.gv.DataBind();
        
        }

        private void CheckOperateList(DataTable dt)
        {
            foreach (DataRow dr in dt.Rows)
            {
                foreach (GridViewRow gvr in this.gv.Rows)
                {
                    Label lblFuncID = gvr.FindControl("lblFuncID") as Label;
                    Label lblSysID = gvr.FindControl("lblSysID") as Label;
                    CheckBoxList chkOperateList = gvr.FindControl("chkOperateList") as CheckBoxList;
                    if (dr[Model.SysFunctions.FIELD_FuncID].ToString() == lblFuncID.Text && dr[Model.SysFunctions.FIELD_SysID].ToString() == lblSysID.Text)
                    {
                        foreach (ListItem item in chkOperateList.Items)
                        {
                            if (dr[SysFuncOperate.FIELD_OperateID].ToString() == item.Value)
                            {
                                item.Selected = true;
                            }
                        }
                    }

                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        private void InitPage()
        {
            divGridView.Visible = false;
            this.ucSysFuncOperates1.TreeInit(SessionBox.CurrentSystem, "System");


            OperateList list = new OperateList();
            Session["SysFuncOperates"] = list.GetDataByKey("");
        }
        protected DataTable GetOperateList()
        {
            DataTable result = Session["SysFuncOperates"] as DataTable;
            return result;
        }

        protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "OperateUpdate")
            {
                GridViewRow gvr = this.gv.Rows[Convert.ToInt32(e.CommandArgument)];


                SysFuncOperate sysFuncOperate = new SysFuncOperate();

                sysFuncOperate.Operates = new List<SysFuncOperate.FuncOperates>();

                sysFuncOperate.DelOperates = new List<SysFuncOperate.FuncOperates>();

                GetFuncOperates(gvr, sysFuncOperate.Operates, sysFuncOperate.DelOperates);
                try
                {
                    sysFuncOperate.InsertTran();
                }
                catch (Exception ex)
                {
                    Jscript.AjaxAlert(this, ex.Message);
                }
                divGridView.Visible = false;
            }
        }

        private void GetFuncOperates(GridViewRow gvr, IList<SysFuncOperate.FuncOperates> result, IList<SysFuncOperate.FuncOperates> delOperateList)
        {
            Label lblFuncID = gvr.FindControl("lblFuncID") as Label;
            Label lblSysID = gvr.FindControl("lblSysID") as Label;
            CheckBoxList chkOperateList = gvr.FindControl("chkOperateList") as CheckBoxList;
            SysFuncOperate.FuncOperates funcOperate = null;
            int i = 0;
            foreach (ListItem item in chkOperateList.Items)
            {
                funcOperate = new SysFuncOperate.FuncOperates();
                funcOperate.FuncID = lblFuncID.Text;
                funcOperate.SysID = lblSysID.Text;
                if (item.Selected)
                {
                    funcOperate.Note = "";
                    funcOperate.OperateID = item.Value;
                    funcOperate.OperateText = item.Text;
                    result.Add(funcOperate);
                }
                if (i == 0)
                {
                    delOperateList.Add(funcOperate);
                    i++;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SysFuncOperate sysFuncOperate = new SysFuncOperate();


            IList<SysFuncOperate.FuncOperates> operateList = new List<SysFuncOperate.FuncOperates>();
            IList<SysFuncOperate.FuncOperates> delOperateList = new List<SysFuncOperate.FuncOperates>();

            foreach (GridViewRow gvr in this.gv.Rows)
            {
                GetFuncOperates(gvr, operateList, delOperateList);
            }
            sysFuncOperate.Operates = operateList;
            sysFuncOperate.DelOperates = delOperateList;
            sysFuncOperate.InsertTran();
            divGridView.Visible = false;
        }

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            CheckBoxList chkOperateList = e.Row.FindControl("chkOperateList") as CheckBoxList;
            if (chkOperateList != null)
            {
                chkOperateList.DataSource = GetOperateList();
                chkOperateList.DataTextField = OperateList.FIELD_OperateName;
                chkOperateList.DataValueField = OperateList.FIELD_OperateID;
                chkOperateList.DataBind();
            }
        }

        protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gv.PageIndex = e.NewPageIndex;
            OperatiOnClick(this.ucSysFuncOperates1.GetSelectedNodeValue);
        }

    }

}
